package com.example.demo2;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/book")
public class BookController {
    @RequestMapping("/getList")
    public List<BookInfo> getList(){
        //从数据库中查询,先mock(模拟)一些数据
        List<BookInfo> bookInfos=mockData();
        for(BookInfo bookInfo: bookInfos){
            if(bookInfo.getStatus()==1){
                bookInfo.setStatusCN("可借阅");//这里的设置是为了方便后面,但不合理,正常后端是返回数字0或1
            }else{
                bookInfo.setStatusCN("不可借阅");
            }
        }
        return bookInfos;
    }

    private List<BookInfo> mockData() {

        List<BookInfo> bookInfos=new ArrayList<>();
        for (int i = 1; i <= 15; i++) {
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(i);
            bookInfo.setBookName("图书"+i);
            bookInfo.setAuthor("作者"+i);
            bookInfo.setPublishName("出版社"+i);
            bookInfo.setNum(new Random().nextInt(100));//生成100以内的随机数
            bookInfo.setPrice(new BigDecimal(new Random().nextInt(70)+10));//10~80之间
            bookInfo.setStatus(i%5==0?0:1);
            bookInfos.add(bookInfo);
        }
        return bookInfos;
    }
}
